package com.mlethe.library.recyclerview.viewholder;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.Icon;
import android.os.Build;
import android.util.SparseArray;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.ColorRes;
import androidx.annotation.DrawableRes;
import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.StringRes;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import java.lang.ref.WeakReference;

/**
 * RecyclerView的ViewHolder
 * Created by Mlethe on 2018/1/11.
 */
public class ViewHolder extends RecyclerView.ViewHolder {

    /**
     * 用来存放子View减少findViewById的次数
     */
    private SparseArray<WeakReference<View>> mViews;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        mViews = new SparseArray<>();
    }

    /**
     * 通过id获取view
     *
     * @param viewId
     * @param <T>
     * @return
     */
    @SuppressWarnings("unchecked")
    public <T extends View> T getView(@IdRes int viewId) {
        WeakReference<View> viewReference = mViews.get(viewId);
        View view = null;
        if (viewReference != null) {
            view = viewReference.get();
        }
        if (view == null) {
            view = itemView.findViewById(viewId);
            mViews.put(viewId, new WeakReference<>(view));
        }
        return (T) view;
    }

    /**
     * 设置背景
     *
     * @param viewId
     * @param background
     * @return
     */
    public ViewHolder setBackground(@IdRes int viewId, Drawable background) {
        View view = getView(viewId);
        if (view != null) {
            view.setBackground(background);
        }
        return this;
    }

    /**
     * 设置背景资源
     *
     * @param viewId
     * @param resid
     * @return
     */
    public ViewHolder setBackgroundResource(@IdRes int viewId, @DrawableRes int resid) {
        View view = getView(viewId);
        if (view != null) {
            view.setBackgroundResource(resid);
        }
        return this;
    }

    /**
     * 设置背景颜色
     *
     * @param viewId
     * @param color
     * @return
     */
    public ViewHolder setBackgroundColorInt(@IdRes int viewId, @ColorInt int color) {
        View view = getView(viewId);
        if (view != null) {
            view.setBackgroundColor(color);
        }
        return this;
    }

    /**
     * 设置背景颜色
     *
     * @param viewId
     * @param colorString
     * @return
     */
    public ViewHolder setBackgroundColor(@IdRes int viewId, String colorString) {
        return setBackgroundColorInt(viewId, Color.parseColor(colorString));
    }

    /**
     * 设置背景颜色
     *
     * @param viewId
     * @param colorId
     * @return
     */
    public ViewHolder setBackgroundColor(@IdRes int viewId, @ColorRes int colorId) {
        View view = getView(viewId);
        if (view != null) {
            view.setBackgroundColor(ContextCompat.getColor(view.getContext(), colorId));
        }
        return this;
    }

    /**
     * 设置alpha
     *
     * @param viewId
     * @param alpha
     * @return
     */
    public ViewHolder setAlpha(@IdRes int viewId, int alpha) {
        View view = getView(viewId);
        if (view != null) {
            view.setAlpha(alpha);
        }
        return this;
    }

    /**
     * 设置Visibility
     *
     * @param viewId
     * @param visibility
     * @return
     */
    public ViewHolder setVisibility(@IdRes int viewId, int visibility) {
        View view = getView(viewId);
        if (view != null) {
            view.setVisibility(visibility);
        }
        return this;
    }

    /**
     * 获取Visibility
     *
     * @param viewId
     * @return
     */
    public int getVisibility(@IdRes int viewId) {
        View view = getView(viewId);
        if (view != null) {
            return view.getVisibility();
        }
        return View.GONE;
    }

    /**
     * 设置Visible
     *
     * @param viewId
     * @param visible false 隐藏，true 显示
     * @return
     */
    public ViewHolder setVisible(@IdRes int viewId, boolean visible) {
        View view = getView(viewId);
        if (view != null) {
            view.setVisibility(visible ? View.VISIBLE : View.GONE);
        }
        return this;
    }

    /**
     * 获取Visible
     *
     * @param viewId
     * @return false 隐藏，true 显示
     */
    public boolean getVisible(@IdRes int viewId) {
        View view = getView(viewId);
        if (view != null) {
            return view.getVisibility() == View.VISIBLE;
        }
        return false;
    }

    /**
     * 设置TextView文本
     *
     * @param viewId
     * @param text
     * @return
     */
    public ViewHolder setText(@IdRes int viewId, CharSequence text) {
        TextView tv = getView(viewId);
        if (tv != null) {
            tv.setText(text);
        }
        return this;
    }

    /**
     * 设置TextView文本
     *
     * @param viewId
     * @param resid
     * @return
     */
    public ViewHolder setText(@IdRes int viewId, @StringRes int resid) {
        TextView tv = getView(viewId);
        if (tv != null) {
            tv.setText(resid);
        }
        return this;
    }

    /**
     * 设置TextView文本
     *
     * @param viewId
     * @param value
     * @return
     */
    public ViewHolder setTextInt(@IdRes int viewId, int value) {
        return setText(viewId, String.valueOf(value));
    }

    /**
     * 设置TextView文本
     *
     * @param viewId
     * @param value
     * @return
     */
    public ViewHolder setText(@IdRes int viewId, float value) {
        return setText(viewId, String.valueOf(value));
    }

    /**
     * 设置TextView文本
     *
     * @param viewId
     * @param value
     * @return
     */
    public ViewHolder setText(@IdRes int viewId, double value) {
        return setText(viewId, String.valueOf(value));
    }

    /**
     * 设置TextView文本
     *
     * @param viewId
     * @param value
     * @return
     */
    public ViewHolder setText(@IdRes int viewId, long value) {
        return setText(viewId, String.valueOf(value));
    }

    /**
     * 设置文字大小
     *
     * @param viewId
     * @param textSize sp
     */
    public ViewHolder setTextSize(@IdRes int viewId, float textSize) {
        TextView tv = getView(viewId);
        if (tv != null) {
            tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
        }
        return this;
    }

    /**
     * 设置文字颜色
     *
     * @param viewId
     * @param color
     */
    public ViewHolder setTextColorInt(@IdRes int viewId, @ColorInt int color) {
        TextView tv = getView(viewId);
        if (tv != null) {
            tv.setTextColor(color);
        }
        return this;
    }

    /**
     * 设置文字颜色
     *
     * @param viewId
     * @param colorId
     */
    public ViewHolder setTextColor(@IdRes int viewId, @ColorRes int colorId) {
        TextView tv = getView(viewId);
        if (tv != null) {
            tv.setTextColor(ContextCompat.getColor(tv.getContext(), colorId));
        }
        return this;
    }

    /**
     * 设置文字颜色
     *
     * @param viewId
     * @param colorString
     */
    public ViewHolder setTextColor(@IdRes int viewId, String colorString) {
        return setTextColorInt(viewId, Color.parseColor(colorString));
    }

    /**
     * 设置TextView提示
     *
     * @param viewId
     * @param text
     * @return
     */
    public ViewHolder setHint(@IdRes int viewId, CharSequence text) {
        TextView tv = getView(viewId);
        if (tv != null) {
            tv.setHint(text);
        }
        return this;
    }

    /**
     * 设置TextView提示
     *
     * @param viewId
     * @param resid
     * @return
     */
    public ViewHolder setHint(@IdRes int viewId, @StringRes int resid) {
        TextView tv = getView(viewId);
        if (tv != null) {
            tv.setHint(resid);
        }
        return this;
    }

    /**
     * 设置TextView提示
     *
     * @param viewId
     * @param value
     * @return
     */
    public ViewHolder setHintInt(@IdRes int viewId, int value) {
        return setHint(viewId, String.valueOf(value));
    }

    /**
     * 设置TextView提示
     *
     * @param viewId
     * @param value
     * @return
     */
    public ViewHolder setHint(@IdRes int viewId, float value) {
        return setHint(viewId, String.valueOf(value));
    }

    /**
     * 设置TextView提示
     *
     * @param viewId
     * @param value
     * @return
     */
    public ViewHolder setHint(@IdRes int viewId, double value) {
        return setHint(viewId, String.valueOf(value));
    }

    /**
     * 设置TextView提示
     *
     * @param viewId
     * @param value
     * @return
     */
    public ViewHolder setHint(@IdRes int viewId, long value) {
        return setHint(viewId, String.valueOf(value));
    }

    /**
     * 设置提示字体颜色
     *
     * @param viewId
     * @param color
     */
    public ViewHolder setHintTextColorInt(@IdRes int viewId, @ColorInt int color) {
        TextView tv = getView(viewId);
        if (tv != null) {
            tv.setHintTextColor(color);
        }
        return this;
    }

    /**
     * 设置文字颜色
     *
     * @param viewId
     * @param colorId
     */
    public ViewHolder setHintTextColor(@IdRes int viewId, @ColorRes int colorId) {
        TextView tv = getView(viewId);
        if (tv != null) {
            tv.setHintTextColor(ContextCompat.getColor(tv.getContext(), colorId));
        }
        return this;
    }

    /**
     * 设置文字颜色
     *
     * @param viewId
     * @param colorString
     */
    public ViewHolder setHintTextColor(@IdRes int viewId, String colorString) {
        return setHintTextColorInt(viewId, Color.parseColor(colorString));
    }

    /**
     * 设置ImageView显示模式
     *
     * @param viewId
     * @param scaleType
     * @return
     */
    public ViewHolder setScaleType(@IdRes int viewId, ImageView.ScaleType scaleType) {
        ImageView iv = getView(viewId);
        if (iv != null) {
            iv.setScaleType(scaleType);
        }
        return this;
    }

    /**
     * 设置ImageView的图片透明
     *
     * @param viewId
     * @param alpha
     * @return
     */
    public ViewHolder setImageAlpha(@IdRes int viewId, int alpha) {
        ImageView iv = getView(viewId);
        if (iv != null) {
            iv.setImageAlpha(alpha);
        }
        return this;
    }

    /**
     * 设置ImageView的图片
     *
     * @param viewId
     * @param bitmap
     * @return
     */
    public ViewHolder setImageBitmap(@IdRes int viewId, Bitmap bitmap) {
        ImageView iv = getView(viewId);
        if (iv != null) {
            iv.setImageBitmap(bitmap);
        }
        return this;
    }

    /**
     * 设置ImageView的图片
     *
     * @param viewId
     * @param icon
     * @return
     */
    public ViewHolder setImageDrawable(@IdRes int viewId, Icon icon) {
        ImageView iv = getView(viewId);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (iv != null) {
                iv.setImageIcon(icon);
            }
        }
        return this;
    }

    /**
     * 设置ImageView的图片
     *
     * @param viewId
     * @param drawable
     * @return
     */
    public ViewHolder setImageDrawable(@IdRes int viewId, Drawable drawable) {
        ImageView iv = getView(viewId);
        if (iv != null) {
            iv.setImageDrawable(drawable);
        }
        return this;
    }

    /**
     * 设置ImageView的资源
     *
     * @param viewId
     * @param resourceId
     * @return
     */
    public ViewHolder setImageResource(@IdRes int viewId, @DrawableRes int resourceId) {
        ImageView iv = getView(viewId);
        if (iv != null) {
            iv.setImageResource(resourceId);
        }
        return this;
    }

    /**
     * 网络图片加载
     *
     * @param viewId
     * @param imageEngine
     * @return
     */
    public ViewHolder setImageByUrl(@IdRes int viewId, ImageEngine imageEngine) {
        ImageView iv = getView(viewId);
        if (iv == null) {
            return this;
        }
        if (imageEngine == null) {
            return this;
        }
        imageEngine.displayImage(iv.getContext(), iv, imageEngine.getImagePath());
        return this;
    }

    /**
     * 设置View的点击事件
     *
     * @param viewId
     * @param listener
     * @return
     */
    public ViewHolder setOnClickListener(@IdRes int viewId, View.OnClickListener listener) {
        View view = getView(viewId);
        if (view != null) {
            view.setOnClickListener(listener);
        }
        return this;
    }

    /**
     * 设置View的长按事件
     *
     * @param viewId
     * @param listener
     * @return
     */
    public ViewHolder setOnLongClickListener(@IdRes int viewId, View.OnLongClickListener listener) {
        View view = getView(viewId);
        if (view != null) {
            view.setOnLongClickListener(listener);
        }
        return this;
    }

    /**
     * 设置条目点击事件
     */
    public ViewHolder setOnItemClickListener(View.OnClickListener listener) {
        itemView.setOnClickListener(listener);
        return this;
    }

    /**
     * 设置条目长按事件
     */
    public ViewHolder setOnItemLongClickListener(View.OnLongClickListener listener) {
        itemView.setOnLongClickListener(listener);
        return this;
    }

    /**
     * 图片加载，这里稍微处理得复杂一些，因为考虑加载图片的第三方可能不太一样
     * 也可以不写这个类
     */
    public static abstract class ImageEngine {
        private String mImagePath;

        public ImageEngine(String imagePath) {
            this.mImagePath = imagePath;
        }

        public String getImagePath() {
            return mImagePath;
        }

        public abstract void displayImage(Context context, ImageView imageView, String imagePath);
    }
}
